package com.dmwork.service.interaction.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dmwork.common.base.result.R;
import com.dmwork.common.base.result.ResultCodeEnum;
import com.dmwork.service.interaction.entity.CmsColor;
import com.dmwork.service.interaction.entity.vo.CmsColorVo;
import com.dmwork.service.interaction.service.CmsColorService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 网页颜色数据表 前端控制器
 * </p>
 *
 * @author 李天
 * @since 2021-10-24
 */

@Transactional
@RestController
@RequestMapping("/admin/dm_work/interaction/cms_color")
public class AdminCmsColorController {

    @Autowired
    private CmsColorService cmsColorService;

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("添加颜色信息")
    @PostMapping("add_color")
    public R addColor(@RequestBody CmsColor cmsColor) {
        int count = cmsColorService.count(new QueryWrapper<CmsColor>().eq("color_key", cmsColor.getColorKey()));
        if (count == 0) {
            return cmsColorService.save(cmsColor) ? R.ok() : R.error();
        } else {
            return R.error().message("该颜色名称已存在");
        }
    }

    @ApiOperation("修改颜色信息")
    @PutMapping("modify_color")
    public R modifyColor(@RequestBody CmsColor cmsColor) {
        CmsColor originalData = cmsColorService.getById(cmsColor.getId());
        if (originalData == null) {
            return R.error().message("颜色数据不存在");
        }
        if (cmsColorService.updateById(cmsColor)) {
            if (redisTemplate.hasKey(originalData.getColorKey())) {
                if (!originalData.getColorKey().equals(cmsColor.getColorKey())) {
                    redisTemplate.delete(originalData.getColorKey());
                }
                redisTemplate.opsForValue().set(cmsColor.getColorKey(), cmsColor.getColorValue());
            }
            return R.ok().message("更新成功");
        } else {
            return R.error().message("更新失败");
        }
    }

    @ApiOperation("删除颜色")
    @DeleteMapping("remove_color/{id}")
    public R removeColor(@PathVariable String id) {
        return cmsColorService.removeById(id) ? R.ok() : R.error();
    }

    @ApiOperation("查询颜色信息列表")
    @GetMapping("get_color_list/{page}/{limit}")
    public R getColorList(@PathVariable Integer page, @PathVariable Integer limit) {
        Page<CmsColorVo> resultPage = cmsColorService.selectPage(page, limit);
        return R.ok().data("total", resultPage.getTotal()).data("rows", resultPage.getRecords());
    }

    @ApiOperation("根据id查询指定颜色")
    @GetMapping("get_color/{id}")
    public R getColor(@PathVariable String id) {
        CmsColor cmsColor = cmsColorService.getById(id);
        CmsColorVo colorVo = null;
        if (cmsColor != null) {
            colorVo = new CmsColorVo();
            BeanUtils.copyProperties(cmsColor, colorVo);
        }
        return R.ok().data("item", colorVo);
    }

}

